package com.ruoyi.web.controller.product;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shop.modules.domain.ProductCategory;
import com.ruoyi.shop.modules.service.IProductCategoryService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品分类Controller
 *
 * @author zjc
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/product/category")
public class ProductCategoryController extends BaseController {
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private ISysDictDataService sysDictDataService;


    /**
     * 查询商品分类列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductCategory productCategory) {
        startPage();
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        return getDataTable(list);
    }

    /**
     * 查询顶级分类
     */
    @GetMapping("/list/top")
    public AjaxResult listTop() {
        ProductCategory category = new ProductCategory();
        category.setParentId(0L);
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(category);
        return AjaxResult.success(list);
    }
    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('product:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCategory productCategory) {
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        ExcelUtil<ProductCategory> util = new ExcelUtil<ProductCategory>(ProductCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productCategoryService.selectProductCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategory productCategory) {
        //查询数据库中是否有该分类
        ProductCategory category = productCategoryService.selectProductCategoryByName(productCategory.getName());
        //没有
        if (category == null) {
            //如果父分类ID为0
            if (productCategory.getParentId() == 0) {
                //添加字典
                SysDictData sysDictData = getSysDictData(productCategory);
                sysDictDataService.insertDictData(sysDictData);
            } else {
                return AjaxResult.error("该分类已存在");
            }
        }
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    private static SysDictData getSysDictData(ProductCategory productCategory) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictSort(0L);
        sysDictData.setIsDefault("N");
        sysDictData.setDictValue(productCategory.getId().toString());
        sysDictData.setDictLabel(productCategory.getName());
        sysDictData.setRemark("商品分类字典");
        sysDictData.setStatus("0");
        sysDictData.setCreateBy(SecurityUtils.getUsername());
        sysDictData.setDictType("product_category");
        return sysDictData;
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCategory productCategory) {
        return toAjax(productCategoryService.updateProductCategory(productCategory));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productCategoryService.deleteProductCategoryByIds(ids));
    }
}
